export default class CoinArea2D extends godot.Area2D {
    constructor(x = 200,y=200,img = ""){
        super();
    
        // 1 设置Area2D碰撞对象的位置
        this.position = new godot.Vector2(x,y);

        // 2 设置Area2D外观Sprite
        let spr = new godot.Sprite();
        if(img){
            spr.texture = godot.load(img) as godot.Texture;
        }else{
            spr.texture = godot.load("res://assets/coin.png") as godot.Texture;
        }
        this.add_child(spr);

        // 3 设置Area2D的碰撞形状(包围盒)
        let shape = new godot.RectangleShape2D();
        let w = spr.texture.get_width();
        let h = spr.texture.get_height();
        shape.extents = new godot.Vector2(w/2,h/2);

        let oid = this.create_shape_owner(this);
        this.shape_owner_add_shape(oid,shape);
    }

    setConnect(){
        // 启动碰撞检测
        this.input_pickable = true;

        // 鼠标进入区域检测
        this.connect(godot.CollisionObject2D.mouse_entered,() => {
            console.log("mouse_entered");
        })
        this.connect(godot.CollisionObject2D.mouse_exited,() => {
            console.log("mouse_exited");
        })

        // Area碰撞
        this.connect(godot.Area2D.area_entered,()=>{
            console.log("area_entered");
        })
        this.connect(godot.Area2D.area_exited,()=>{
            console.log("area_exited");
        })

        this.connect(godot.Area2D.area_shape_entered,()=>{
            console.log("area_shape_entered");
        })
        this.connect(godot.Area2D.area_shape_exited,()=>{
            console.log("area_shape_exited");
        })


        // 注册与Body碰撞响应
        this.connect(godot.Area2D.body_entered,()=>{
            console.log("body_entered");
        })
        this.connect(godot.Area2D.body_exited,()=>{
            console.log("body_exited");
        })
        this.connect(godot.Area2D.body_shape_entered,()=>{
            console.log("body_shape_entered");
        })
        this.connect(godot.Area2D.body_shape_exited,()=>{
            console.log("body_shape_exited");
        })
    }
}